import { BUNDLE_TYPE_ENUM } from "../../base/EnumIndex";
import LvItem from "../../LvItem";
import { AudioManager1 } from "../../manager/AudioManager1";
import { UIManager } from "../../manager/UIManager";
import Level_24_Base from "./Level_24_Base";

const { ccclass, property } = cc._decorator;
let QUESION = [];
@ccclass
export default class Level_24_1 extends Level_24_Base {
    @property(dragonBones.ArmatureDisplay)
    lightSkeMain1: dragonBones.ArmatureDisplay = null;


    @property(dragonBones.ArmatureDisplay)
    lightSkeMain2: dragonBones.ArmatureDisplay = null;


    @property(cc.Node)
    zhan: cc.Node = null;

    gN(NodeName: string) {
        if (!this.levelPre.getNodeForName(NodeName).getComponent(LvItem)) {
            this.levelPre.getNodeForName(NodeName).addComponent(LvItem)
        }
        return this.levelPre.getNodeForName(NodeName);
    }
    gNa(NodeName: string, active: boolean) {
        this.levelPre.getNodeForName(NodeName).active = active;
    }

    finish(finishStr: any) {
        if (typeof finishStr != "string") {
            return;
        }

        if (finishStr.includes("ans")) {
            this.mainSpine.setAnimation(0, "reng", false)
            let progress = Number(finishStr.split("_")[2]);
            let answer = Number(finishStr.split("_")[3]);
            this.levelPre.content.active = false;
            this.checkAnswer(progress, answer - 1);
        } else if (finishStr === "initGame") {
            this.initGame();
        }
        if (finishStr.includes("removeWidget")) {
            this.gN("qCon").removeComponent(cc.Widget)
        }
    }

    getDelayTime(isRight: boolean) {
        return 2//isRight ? 2 : 2;
    }
    /** 开始一轮游戏 */
    async startGame() {
        QUESION = this.levelPre.interJson.questions;
        cc.log("当前第" + this.levelPre.progress + "题")
        this.resetAnim();
        this.scheduleOnce(async () => {

            UIManager.ins.enabledGameClick(false);
            this.npcTalk.active = true;
            this.npcTalk.getComponent(LvItem).setLabString(QUESION[this.levelPre.progress]);
            this.npcSpine.setAnimation(0, "shuohua", true);
            this.talkAnim();
            let audioId = await AudioManager1.playAudio("" + (1 + this.levelPre.progress), 1, BUNDLE_TYPE_ENUM.LEVEL);
            cc.Tween.stopAllByTarget(this.npcTalk);
            let audioTime = cc.audioEngine.getDuration(audioId);
            this.npcTalkEnd()
        }, 1)
    }
    resetAnim() {
        this.lightSkeMain1.node.active = false;
        this.lightSkeMain2.node.active = false;
        this.mainSpine.node.active = true;
        this.npcSpine.node.opacity = 0;


        cc.tween(this.zhan)
            .by(1, { x: 300 })
            .call(() => {
                this.npcSpine.node.opacity = 255;
                this.zhan.x = -792.753
                if (this.levelPre.progress < 7) {
                    cc.tween(this.zhan)
                        .by(1, { x: 300 })
                        .start();
                }
            })
            .start()

        if (this.mainSpine.animation != "daiji") this.mainSpine.setAnimation(0, "daiji", true);
        if (this.npcSpine.animation != "daiji") this.npcSpine.setAnimation(0, "daiji", true);


    }
    talkAnim() {
        this.mainSpine.setAnimation(0, "daiji", true)
    }
    rightAnim() {
        AudioManager1.playAudio("xuandui", 1, BUNDLE_TYPE_ENUM.GAME);
        AudioManager1.playAudio("电击", 0.2, BUNDLE_TYPE_ENUM.LEVEL);
        AudioManager1.playAudio("不要啊！", 1, BUNDLE_TYPE_ENUM.LEVEL);
        this.npcTalk.getComponent(LvItem).setLabString("不要啊！");
        this.npcTalk.active = true;
        cc.Tween.stopAllByTarget(this.mainTalk);
        this.lightSkeMain2.node.active = true;
        this.lightSkeMain2.playAnimation("newAnimation", 0);

        this.mainSpine.setAnimation(0, "xiao", true);
        this.npcSpine.setAnimation(0, "dian", true);
        this.scheduleOnce(() => {
            this.lightSkeMain2.node.active = false;
            this.mainSpine.setAnimation(0, "daiji", true);
            this.npcSpine.setAnimation(0, "daiji", true);
            this.npcTalk.active = false;
        }, 1.8);
    }
    wrongAnim1() {
        AudioManager1.playAudio("电击", 0.2, BUNDLE_TYPE_ENUM.LEVEL);
        AudioManager1.playAudio("整挺好啊", 1, BUNDLE_TYPE_ENUM.LEVEL);
        this.npcTalk.getComponent(LvItem).setLabString("整挺好啊");
        this.npcTalk.active = true;
        this.lightSkeMain1.node.active = true;
        this.lightSkeMain1.playAnimation("newAnimation", 0);

        this.npcSpine.setAnimation(0, "xiao", true);


        this.mainSpine.setAnimation(0, "dian", true);
        this.scheduleOnce(() => {
            this.lightSkeMain1.node.active = false;
            this.mainSpine.setAnimation(0, "daiji", true);
            this.npcSpine.addAnimation(0, "daiji", true);
            this.npcTalk.active = false;
        }, 1.8);
    }

    wrongAnim2() {
        AudioManager1.playAudio("电击", 0.2, BUNDLE_TYPE_ENUM.LEVEL);
        AudioManager1.playAudio("你听不听得懂人话啊！", 1, BUNDLE_TYPE_ENUM.LEVEL);
        this.npcTalk.getComponent(LvItem).setLabString("你听不听得懂人话啊！");
        this.npcTalk.active = true;
        this.lightSkeMain1.node.active = true;
        this.lightSkeMain1.playAnimation("newAnimation", 1);

        this.npcSpine.setAnimation(0, "shengqi", true);

        this.mainSpine.setAnimation(0, "dian", true);
        this.scheduleOnce(() => {
            this.lightSkeMain1.node.active = false;
            this.mainSpine.setAnimation(0, "daiji", true);
            this.npcSpine.addAnimation(0, "daiji", true);
            this.npcTalk.active = false;
        }, 1.8);
    }
}